﻿using System;
using System.Data;

using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmAdvancePaymentList : DockContent
    {
        BranchClient _wcfBranch;
        DepartmentClient _wcfDepartment;
        AdvancePaymentClient _wcfAdvance;

        int _month = DateTime.Now.Month;
        int _year = DateTime.Now.Year;
        int _AdvanceID;
        int _BranchID;
        int _DepartmentID;
        int _EmployeeID;
        string _keyword;
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        bool _loadedData = false;
        RoleRightInfo listRole;

        public frmAdvancePaymentList()
        {

            InitializeComponent();
            Language();
            _wcfAdvance = Service.AdvancePayment();
            _wcfBranch = Service.Branch();
            _wcfDepartment = Service.Department();
            listRole = UIParamater.Quyen_TamUngLuong;


            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                this.btnEdit.Tag = "xem";
            }

            UISetting.LoadMonths(ref cboMonth);
            cboMonth.Text = DateTime.Now.Month.ToString();
            UISetting.LoadYears(ref cboYear);
            //cboYear.ComboBox.SelectedValue = Convert.ToUInt32(DateTime.Now.Year);
            UISetting.LoadTreeView3(ref tvBranch);
            LoadData();
        }

        private void Language()
        {
            this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }


        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            txtKeyWord.Text = "";
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                {
                    _BranchID = 0;
                    _DepartmentID = 0;
                }
                else
                    _BranchID = Program.intBranchID;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                _BranchID = Convert.ToInt32(e.Node.Tag);
                _DepartmentID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level >= 2)
            {
                _DepartmentID = Convert.ToInt32(e.Node.Tag);
                _BranchID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();
            }
        }

        private void LoadData()
        {

            _month = Convert.ToInt32(cboMonth.Text);
            int temptYear;
            if (Int32.TryParse(cboYear.Text, out temptYear))
                _year = temptYear;

            if (txtKeyWord.Text.Trim().Contains("'"))
                _keyword = txtKeyWord.Text.Trim().Replace("'", "''");
            else
                _keyword = txtKeyWord.Text.Trim();

            DataTable dt = _wcfAdvance.AdvancePayment_GetList(out itemCount, currPage, pageSize, _BranchID, _DepartmentID, _keyword, _month, _year).Tables[0];

            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();

            dgvList.DataSource = dt;
            dgvList.Columns["AdvancePayment_ID"].Visible = false;
            dgvList.Columns["AdvancePayment_EmployeeID"].Visible = false;
            dgvList.Columns["AdvancePayment_BranchID"].Visible = false;
            dgvList.Columns["AdvancePayment_DepartmentID"].Visible = false;
            dgvList.Columns["RowNumber"].HeaderText = UpdateUI.GetString("stt");
            dgvList.Columns["RowNumber"].Width = 50;
            dgvList.Columns["FullName"].HeaderText = UpdateUI.GetString("hovaten");
            dgvList.Columns["FullName"].Width = 150;
            dgvList.Columns["AdvancePayment_Position"].HeaderText = UpdateUI.GetString("chucvu");
            dgvList.Columns["AdvancePayment_Position"].Width = 130;
            dgvList.Columns["AdvancePayment_Date"].HeaderText = UpdateUI.GetString("ngay");
            dgvList.Columns["AdvancePayment_Date"].DefaultCellStyle.Format = "dd/MM/yyyy";
            dgvList.Columns["AdvancePayment_Date"].Width = 100;
            dgvList.Columns["AdvancePayment_Amount"].HeaderText = UpdateUI.GetString("sotien");
            dgvList.Columns["AdvancePayment_Amount"].DefaultCellStyle.Format = UIParamater.Phay2So;
            dgvList.Columns["AdvancePayment_Amount"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
            dgvList.Columns["AdvancePayment_Amount"].Width = 100;
            dgvList.Columns["AdvancePayment_DinhKy"].HeaderText = UpdateUI.GetString("dinhky");
            dgvList.Columns["AdvancePayment_DinhKy"].Width = 90;
            dgvList.Columns["AdvancePayment_AmountPayment"].HeaderText = UpdateUI.GetString("sotientrahangthang");
            dgvList.Columns["AdvancePayment_AmountPayment"].DefaultCellStyle.Format = UIParamater.Phay2So;
            dgvList.Columns["AdvancePayment_AmountPayment"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
            dgvList.Columns["AdvancePayment_AmountPayment"].Width = 200;
            dgvList.Columns["AdvancePayment_Description"].HeaderText = UpdateUI.GetString("ghichu");
            dgvList.Columns["AdvancePayment_Description"].Width = 200;
            _loadedData = true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmAdvancePaymentInfo(0, _EmployeeID);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {

                    _AdvanceID = Convert.ToInt32(dgvList.CurrentRow.Cells["AdvancePayment_ID"].Value);
                    _EmployeeID = Convert.ToInt32(dgvList.CurrentRow.Cells["AdvancePayment_EmployeeID"].Value);
                    Form f = new frmAdvancePaymentInfo(_AdvanceID, _EmployeeID);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _AdvanceID = Convert.ToInt32(dgvList.CurrentRow.Cells["AdvancePayment_ID"].Value);
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfAdvance.AdvancePayment_Delete(_AdvanceID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage > 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmAdvancePaymentList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadedData)
            {
                currPage = 0;
                LoadData();
            }

        }

        private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (_loadedData)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void frmAdvancePaymentList_Activated(object sender, EventArgs e)
        {
            Language();
            dgvList.Columns["RowNumber"].HeaderText = UpdateUI.GetString("stt");
            dgvList.Columns["FullName"].HeaderText = UpdateUI.GetString("hovaten");
            dgvList.Columns["AdvancePayment_Position"].HeaderText = UpdateUI.GetString("chucvu");
            dgvList.Columns["AdvancePayment_Date"].HeaderText = UpdateUI.GetString("ngay");
            dgvList.Columns["AdvancePayment_Amount"].HeaderText = UpdateUI.GetString("sotien");
            dgvList.Columns["AdvancePayment_DinhKy"].HeaderText = UpdateUI.GetString("dinhky");
            dgvList.Columns["AdvancePayment_AmountPayment"].HeaderText = UpdateUI.GetString("sotientrahangthang");
            dgvList.Columns["AdvancePayment_Description"].HeaderText = UpdateUI.GetString("ghichu");
        }

        private void frmAdvancePaymentList_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfAdvance.Close();
            _wcfBranch.Close();
            _wcfDepartment.Close();
        }
    }
}
